package cn.stu.framework.service.impl;


import cn.stu.common.utils.ApiData;
import cn.stu.framework.entity.Announcement;
import cn.stu.framework.mapper.AnnouncementMapper;
import cn.stu.framework.service.AnnouncementService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 公告服务实现类
 *
 * @author 郑志豪
 * @date 2023/09/18
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    /**
     * 列表
     *
     * @param pageNumber 页码
     * @param pageSize   页面大小
     * @return {@link ApiData}
     */
    @Override
    @Cacheable(value = "AnnouncementList")
    public ApiData list(Long pageNumber, Long pageSize) {
        Page<Announcement> page = new Page<>(pageNumber, pageSize);
        lambdaQuery().orderByDesc(Announcement::getCreateTime).page(page);
        return ApiData.getPageTrue("获取成功", page.getTotal(), page.getRecords());
    }


    /**
     * 公告创建
     *
     * @param announcement 公告
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "AnnouncementList", allEntries = true)
    public ApiData announcementCreate(Announcement announcement) {
        baseMapper.insert(announcement);
        return ApiData.getTrue("添加成功");
    }

    /**
     * 公告更新
     *
     * @param announcement 公告
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "AnnouncementList", allEntries = true)
    public ApiData announcementUpdate(Announcement announcement) {
        baseMapper.updateById(announcement);
        return ApiData.getTrue("修改成功");
    }

    /**
     * 公告删除
     *
     * @param id ID
     * @return {@link ApiData}
     */
    @Override
    @CacheEvict(value = "AnnouncementList", allEntries = true)
    public ApiData announcementDelete(Long id) {
        baseMapper.deleteById(id);
        return ApiData.getTrue("删除成功");
    }
}